home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / COOLVIEW.ZIP / BINHEX.PAS < prev    next >
Pascal/Delphi Source File  |  1995-03-04  |  2KB  |  104 lines

  1. { BiNHEX unit (c) 1995 by Paradise }
  2. { (a.k.a. Marcin Jaskowiak)        }
  3. { paradise@bachus.umcs.lublin.pl   }
  4. { paradise@ajax.umcs.lublin.pl     }
  5. { paradise@anon.penet.fi           }
  6. {                                  }
  7. { Conversions :                    }
  8. {  BYTE -> BIN                     }
  9. {  WORD -> BIN                     }
  10. {  BYTE -> HEX                     }
  11. {  WORD -> HEX                     }
  12. {  BYTE -> DEC                     }
  13. {  WORD -> DEC                     }
  14. {  BYTE -> ASC                     }
  15. {                                  }
  16. { You can use this piece of code   }
  17. { in your programs, but remember   }
  18. { to credit or greet me :)         }
  19. {                                  }
  20.  
  21. unit BINHEX;
  22.  
  23. {■■■}   interface    {■■■}
  24.  
  25. function Word2Bin(W: Word): string;
  26. function Byte2Bin(B: Byte): string;
  27. function Word2Hex(W: Word): string;
  28. function Byte2Hex(B: Byte): string;
  29. function Word2Dec(W: Word): string;
  30. function Byte2Dec(B: Byte): string;
  31. function Byte2Asc(B: Byte): string;
  32.  
  33. {■■■} implementation {■■■}
  34.  
  35. const
  36.  Digits : string ='0123456789ABCDEF';
  37.  
  38. function Word2Bin(W: Word): string;
  39. var
  40.   s : String;
  41.   i,j,k: Word;
  42. begin
  43.   k:=1; s:='';
  44.   for i:=$0 to $f do
  45.   begin
  46.     if (W and k)>0 then s:='1'+s
  47.     else s:='0'+s;
  48.     k:=k*2;
  49.   end;
  50.   Word2Bin:=s+'b';
  51. End;
  52.  
  53. function Byte2Bin(B: Byte): string;
  54. var
  55.   s : String;
  56.   i,j,k: Word;
  57. begin
  58.   k:=1; s:='';
  59.   for i:=$0 to $7 do
  60.   begin
  61.     if (B and k)>0 then s:='1'+s
  62.     else s:='0'+s;
  63.     k:=k*2;
  64.   end;
  65.   Byte2Bin:=s+'b';
  66. End;
  67.  
  68. function Word2Hex(W: Word): string;
  69. begin
  70.   Word2Hex:='0'+Digits[hi(W) and $f0 shr 4+1]+Digits[hi(W) and $0f+1]
  71.            +Digits[lo(W) and $f0 shr 4+1]+Digits[lo(W) and $0f+1]+'h';
  72. End;
  73.  
  74. function Byte2Hex(B: Byte): string;
  75. begin
  76.   Byte2Hex:='0'+Digits[B and $f0 shr 4+1]+Digits[B and $0f+1]+'h';
  77. End;
  78.  
  79. function Word2Dec(W: Word): string;
  80. var
  81.   ss : string;
  82. begin
  83.   Str(W,ss);
  84.   Word2Dec:=ss;
  85. end;
  86.  
  87. function Byte2Dec(B: Byte): string;
  88. var
  89.   ss : string;
  90. begin
  91.   Str(B,ss);
  92.   Byte2Dec:=ss;
  93. end;
  94.  
  95. function Byte2Asc(B: Byte): string;
  96. begin
  97.   Byte2Asc:=''''+Chr(B)+'''';
  98. end;
  99.  
  100.  
  101. { end of code! }
  102. end.
  103.  
  104.